package com.test.deencredit.adapter_deencredit;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.deencredit.R;
import com.test.deencredit.bean_deencredit.config_deencredit.AccountChannel_Fr_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AccountChannelViewAdapter_deencredit extends RecyclerView.Adapter<AccountChannelViewAdapter_deencredit.AccountChannelHolder_deencredit> {

    private final ArrayList<AccountChannel_Fr_deencredit> channels_deencredit;

    private final String page_deencredit;

    private AccountChannel_Fr_deencredit selectChannel_deencredit;

    private final ArrayList<AccountChannelHolder_deencredit> holderList_deencredit = new ArrayList<>();

    public AccountChannelViewAdapter_deencredit(ArrayList<AccountChannel_Fr_deencredit> channels, String page){
        this.channels_deencredit = channels;
        this.page_deencredit = page;
    }

    public AccountChannel_Fr_deencredit getSelectChannel_deencredit() {
        return selectChannel_deencredit;
    }

    @NonNull
    @Override
    public AccountChannelHolder_deencredit onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder_deencredit(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_channel_fr_deencredit,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder_deencredit holder, int position) {

        if (!holderList_deencredit.contains(holder)) holderList_deencredit.add(holder);

        AccountChannel_Fr_deencredit channel = channels_deencredit.get(position);

        holder.setChannelDesc_deencredit(channel.getDesc_deencredit());

        holder.setClick_deencredit(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DIALOG_TYPE_CLICK",channel.getDesc_deencredit());
            selectChannel_deencredit = channel;
            for (AccountChannelHolder_deencredit holder1: holderList_deencredit){
                holder1.setCheck_deencredit(holder1 == holder);
            }
        });

        //获取图标
        if (channel.getBitmap_deencredit()==null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(channel.getLogo_deencredit())) return;
                    URL url = new URL(channel.getLogo_deencredit());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        channel.setBitmap_deencredit(bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> holder.setLogo_deencredit(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else holder.setLogo_deencredit(channel.getBitmap_deencredit());

    }

    @Override
    public int getItemCount() {
        return channels_deencredit.size();
    }

    public static class AccountChannelHolder_deencredit extends RecyclerView.ViewHolder{

        private final View clickView;
        private final ImageView checkIv;
        private final ImageView logoIv;
        private final TextView channelTv;

        private final int colorBlue,colorGray;

        @SuppressLint("InflateParams")
        public AccountChannelHolder_deencredit(View view){

            super(view);

            clickView = view.findViewById(R.id.iacf_view_click);
            checkIv = view.findViewById(R.id.iacf_iv_check);
            logoIv = view.findViewById(R.id.iacf_iv_logo);
            channelTv = view.findViewById(R.id.iacf_tv_channel);

            colorBlue = view.getContext().getColor(R.color.purple_main_deencredit);
            colorGray = Color.parseColor("#FF6E6E6E");

        }

        public void setClick_deencredit(View.OnClickListener clickListener){
            if (clickView!=null) clickView.setOnClickListener(clickListener);
        }

        public void setCheck_deencredit(boolean isCheck){
            if (isCheck){
                checkIv.setBackgroundResource(R.drawable.icon_check_true_deencredit);
                clickView.setBackgroundResource(R.drawable.bg_purple_t_rc10dp_border_purple_1dp_deencredit);
                channelTv.setTextColor(colorBlue);
            }else {
                checkIv.setBackgroundResource(R.drawable.icon_check_false_deencredit);
                clickView.setBackgroundResource(R.drawable.bg_gray_rc10dp_deencredit);
                channelTv.setTextColor(colorGray);
            }
        }

        public void setLogo_deencredit(Bitmap bitmap){
            logoIv.setImageBitmap(bitmap);
        }

        public void setChannelDesc_deencredit(String text){
            channelTv.setText(text);
        }



    }
}
